/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.customize.mysql.utils.ExportExcelUtils;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.user.dto.AppWithdrawalRecordPageDTO;
import com.pig4cloud.pigx.user.dto.ApplyWithdrawalDTO;
import com.pig4cloud.pigx.user.dto.WithdrawalRecordPageDTO;
import com.pig4cloud.pigx.user.dto.WithdrawalToExamineDTO;
import com.pig4cloud.pigx.user.entity.WithdrawalRecord;
import com.pig4cloud.pigx.user.enums.WalletStatusEnums;
import com.pig4cloud.pigx.user.service.WithdrawalRecordService;
import com.pig4cloud.pigx.user.vo.AdminPageDemoVO;
import com.pig4cloud.pigx.user.vo.LoginUserWithdrawalRecordVO;
import com.pig4cloud.pigx.user.vo.UserWithdrawalInfoVO;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 提现记录
 *
 * @author pigx code generator
 * @date 2022-11-28 16:16:25
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/withdrawal_record" )
@Api(value = "withdrawal_record", tags = "提现记录管理")
public class WithdrawalRecordController {

    private final  WithdrawalRecordService withdrawalRecordService;


    /**
     * 分页查询
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/page" )
    @PreAuthorize("@pms.hasPermission('user_withdrawalrecord_view')" )
    public R<AdminPageDemoVO<WithdrawalRecord>> getWithdrawalRecordPage(@RequestBody WithdrawalRecordPageDTO dto) {
		// 筛选条件
		return withdrawalRecordService.getWithdrawalRecordPage(dto);
    }

	@GetMapping(value = "/excel", produces = "application/octet-stream")
	public R<String> excel(WithdrawalRecordPageDTO dto, HttpServletResponse response) {
		HSSFWorkbook wb = withdrawalRecordService.excel(dto);
		OutputStream out = null;
		try {
			out = response.getOutputStream();
			response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
			//代表的是Excel文件类型
			response.setHeader("Content-Disposition", "attachment;filename=duckweed提现记录.xls");
			wb.write(out);
			return R.ok("导出成功");
		} catch (IOException e) {
			return R.failed("导出失败");
		} finally {
			try {
				assert out != null;
				out.flush();
				out.close();
				wb.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 获取用户提现记录
	 * @param dto dto
	 * @return r
	 */
	@PostMapping("/app/page")
	public R<Page<LoginUserWithdrawalRecordVO>> getAppPage(@RequestBody AppWithdrawalRecordPageDTO dto) {
		return withdrawalRecordService.getAppPage(dto);
	}

	/**
	 * 用户提现申请
	 * @param dto dto
	 * @return r
	 */
	@PostMapping("/apply")
	public R<String> create(@RequestBody ApplyWithdrawalDTO dto) {
		return withdrawalRecordService.create(dto);
	}

	/**
	 * 提现审核用户信息
	 * @param id userId
	 * @return r
	 */
	@GetMapping("/user/info/{id}")
	public R<UserWithdrawalInfoVO> getUserInfo(@PathVariable("id" ) Long id) {
		return withdrawalRecordService.getUserInfo(id);
	}

	/**
	 * 划转
	 * @param num 数量
	 * @return r
	 */
	@GetMapping("/transfer")
	public R<String> transfer(@RequestParam("num") BigDecimal num) {
		return withdrawalRecordService.transfer(num);
	}

	/**
	 * 提现后台审核
	 * @param dto dtp
	 * @return r
	 */
	@PostMapping("/examine")
	public R<String> toExamine(@RequestBody WithdrawalToExamineDTO dto) {
		return withdrawalRecordService.toExamine(dto);
	}

    /**
     * 通过id查询提现记录
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_withdrawalrecord_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(withdrawalRecordService.getById(id));
    }

    /**
     * 新增提现记录
     * @param withdrawalRecord 提现记录
     * @return R
     */
    @ApiOperation(value = "新增提现记录", notes = "新增提现记录")
    @SysLog("新增提现记录" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('user_withdrawalrecord_add')" )
    public R save(@RequestBody WithdrawalRecord withdrawalRecord) {
        return R.ok(withdrawalRecordService.save(withdrawalRecord));
    }

    /**
     * 修改提现记录
     * @param withdrawalRecord 提现记录
     * @return R
     */
    @ApiOperation(value = "修改提现记录", notes = "修改提现记录")
    @SysLog("修改提现记录" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('user_withdrawalrecord_edit')" )
    public R updateById(@RequestBody WithdrawalRecord withdrawalRecord) {
        return R.ok(withdrawalRecordService.updateById(withdrawalRecord));
    }

    /**
     * 通过id删除提现记录
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除提现记录", notes = "通过id删除提现记录")
    @SysLog("通过id删除提现记录" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_withdrawalrecord_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(withdrawalRecordService.removeById(id));
    }

}
